package com.dup.algms;

import com.dup.factory.MatchAlgmsFactory;
import com.dup.interfaces.MatchEx;

public class SoundMatchEx implements MatchEx {

	String oldStr_s = "";
	String newStr_s = "";

	/*
	 * Implements the mapping 
	 * from: AEHIOUWYBFPVCGJKQSXZDTLMNR 
	 * to: 00000000111122222222334556
	 */
	public static final char[] MAP = {
			// A B C D E F G H I J K L M
			'0', '1', '2', '3', '0', '1', '2', '0', '0', '2', '2', '4', '5',
			// N O P W R S T U V W X Y Z
			'5', '0', '1', '2', '6', '2', '3', '0', '1', '0', '2', '0', '2' };

	@Override
	public boolean machWithWord(String oldStr, String newStr) {
		oldStr_s = soundex(oldStr);
		newStr_s = soundex(newStr);
		MatchAlgmsFactory.setMsg(oldStr_s + " " + oldStr);
		MatchAlgmsFactory.setMsg(newStr_s + " " + newStr);
		return false;
	}

	public static String soundex(String s) {

		String t = s.toUpperCase();
		StringBuffer res = new StringBuffer();
		char c, prev = '?';
		for (int i = 0; i < t.length() && res.length() < 4
				&& (c = t.charAt(i)) != ' '; i++) {
			if (c >= 'A' && c <= 'Z' && c != prev) {
				prev = c;
				if (i == 0)
					res.append(c);
				else {
					char m = MAP[c - 'A'];
					if (m != '0')
						res.append(m);
				}
			}
		}
		if (res.length() == 0)
			return null;
		for (int i = res.length(); i < 4; i++)
			res.append('0');
		return res.toString();
	}

}
